package com.zhongwang.mms.module.tool.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.common.ValidResult;
import com.zhongwang.mms.module.tool.model.AccOffMaterModel;
import com.zhongwang.mms.module.tool.model.ReportAccOffMaterModel;
import com.zhongwang.mms.module.tool.service.AccOffMaterService;
import com.zhongwang.mms.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 办公物资台账
 */
@Controller
public class AccOffMaterController extends BaseController{
    @Autowired
    private AccOffMaterService accOffMaterService;

    /**
     * 页面
     *
     * @return 页面路径
     */
    @GetMapping("/tool/offMater/page")
    @RequiresPermissions({"drive:officematsb:view"})
    public String linkePage() {
        return "tool/offMater/list";
    }

    /**
     * 查询办公物资台账信息
     *
     * @param model 设备台账参数
     * @return EquipmentModel
     */
    @ResponseBody
    @PostMapping("/tool/offMater/list")
    @RequiresPermissions({"drive:officematsb:view"})
    public JsonResult selectAccEquipmentList(AccOffMaterModel model) {
        startPage();
        return jsonResult(accOffMaterService.selList(model));
    }

    /**
     * 办公设备台账-> 导入
     *
     * @param file      导入文件
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/tool/offMater/import")
    @RequiresPermissions({"drive:officematsb:add"})
    public JsonResult dailyTotalImport(@RequestParam("file") MultipartFile file) {
        return success(accOffMaterService.importFile(file));
    }

    /**
     * 删除
     * @param empId 主键id
     * @return
     */
    @PostMapping("/tool/offMater/del")
    @ResponseBody
    @RequiresPermissions({"drive:officematsb:del"})
    public JsonResult deleteByPrimaryKey(Integer empId){
        JsonResult jsonResult = new JsonResult();
        Integer flg=accOffMaterService.del(empId);
        if (flg==1) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("删除成功");
        }
        else{
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("删除失败");
        }
        return jsonResult;
    }

    /**
     * 新增设备台账信息
     */
    @PostMapping("/tool/offMater/add")
    @ResponseBody
    @RequiresPermissions({"drive:officematsb:add"})
    public JsonResult insert(ReportAccOffMaterModel data){
        JsonResult jsonResult = new JsonResult();
        int flg=-1;
        /**
         * 设置人员信息和删除标识
         */
        data.setMatIsdelete(0);

        AccOffMaterModel keyModel = new AccOffMaterModel();
        //根据资产编号查询
        keyModel.setMatCode(data.getMatCode());
        ReportAccOffMaterModel checkModel= accOffMaterService.selectAccOffMater(keyModel);
        if (checkModel != null) {
            //更新参数的model并且赋值
            data.setMatId(checkModel.getMatId());
            //执行修改，不存在序号的问题。
            data.setMatUpdUser(ShiroUtils.getUser().getName());
            flg= accOffMaterService.update(data);
        }else {
            if(data.getMatSortNum()==null) {
                //需要查询最大的序号，间隔为10。
                data.setMatSortNum(accOffMaterService.selectMaxNum());
            }
            data.setMatSortNum(accOffMaterService.selectMaxNum());
            data.setMatCreUser(ShiroUtils.getUser().getName());
            data.setMatUpdUser(ShiroUtils.getUser().getName());
            data.setMatIsdelete(0);
            flg= accOffMaterService.insert(data);
        }
        if (flg==1) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("添加成功");
        }
        else{
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("添加失败");
        }
        return jsonResult;
    }


    /**
     * 新增设备台账信息
     */
    @PostMapping("/tool/offMater/edit")
    @ResponseBody
    @RequiresPermissions({"drive:officematsb:edit"})
    public JsonResult edit(ReportAccOffMaterModel data){
        JsonResult jsonResult = new JsonResult();
        data.setMatUpdUser(ShiroUtils.getUser().getName());
        int flg= accOffMaterService.update(data);
        if(flg==1){
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("添加成功");
        }
        else{
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("添加失败");
        }
        return jsonResult;
    }

    /**
     * 检查同一主键是否存在
     * @param paramModel 类型名称和id
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/tool/offMater/checkUnique")
    public ValidResult checkUnique(AccOffMaterModel paramModel) {
        List<AccOffMaterModel> list = accOffMaterService.findByCode(paramModel);
        if(list.size()>0) {
            //已经存在了。
            return ValidResult.FAIL;
        }
        return ValidResult.SUCCESS;
    }

    /**
     * 判断文件是否存在
     *
     * @param url 文件路径
     * @return
     */
    @ResponseBody
    @PostMapping("/tool/offMater/exist")
    public JsonResult exist(String url) {
        return jsonResult(accOffMaterService.exist(url));
    }
}
